package com.neutech.shop.service.impl;

import com.neutech.shop.entity.Category;
import com.neutech.shop.mapper.CategoryMapper;
import com.neutech.shop.service.CategoryService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @author Neuedu-远哥
 * @version 1.0
 * @date 2021/9/27 9:29
 */
@Service
@Transactional()
public class CategoryServiceImpl implements CategoryService {
    @Resource
    private CategoryMapper mapper;
    @Override
    public void insertRootCategory(String name, String descr) {
        mapper.insertRootCategory(name,descr);
    }

    @Override
    public List<Category> selectAll() {
        return mapper.selectAll();
    }
    @Override
    public Category selectByParentId(Integer pid){
        return mapper.selectByParentId(pid);
    }

    @Override
    public void insertChildCategory(String name, String descr, Integer pid) {
        // 业务处理
        // 1 先查
        Category category = mapper.selectByParentId(pid);
        // 2 更新叶子节点为非叶子节点
        mapper.updateParentLeaf(pid);
        category.setName(name);
        category.setDescr(descr);
        category.setPid(pid);
        category.setGrade(category.getGrade() + 1);
        // 3 添加
        mapper.insertChildCategory(category);
    }

    @Override
    public List<Category> findThirdGrade() {
        return mapper.selectThirdGrade();
    }
}
